<?php
 
namespace app\controllers;

use Yii; // 导入Yii
use yii\web\Controller; // 导入Controller
use app\controllers\BaseController;
use app\common\Deal;
use app\models\GameUser;



class InitController extends Controller{
	public $enableCsrfValidation = false;

	public function actionLogin()
	{
		$wxdeal = new Deal();
		$redis = Yii::$app->redis;
        if( Yii::$app->request->isPost ){
            $postData = Yii::$app->request->post();
        	$code = $postData['code'];
                $session = $postData['session'];
        	if( $session != Null ){
        		$logininfo = Yii::$app->redis->del('3rdsession'.$session);
        	}
            //获取用户的openid
        	$logininfo = $wxdeal->getLogininfo($code);
        	$session_key = $logininfo['session_key'];
                $openid = $logininfo['openid'];
            //检查session_key是否存在
            if( !isset($session_key) ){
                $content = "没有找到session_key,请尝试重新登录";
                $data = array(
                    '0' => -3,
                    '1' => 'session_key',
                );
                return BaseController::FailRespone($data);
                exit(0);
            }

            //redis储存用户信息
            $redisinfo = array(
                'openid' => $openid,
                'session_key' =>  $session_key,
            );
            $session = $wxdeal->_3rd_session(32);
            $key = '_3rd_session:' . $session;
            $redis->setex($key, 1209600, json_encode($redisinfo));

            //用户新旧判断
            $user = GameUser::judgeOpenId($openid);
            if( $user ){
                $data = array(
                    'login' => true,//已经登录
                    'session' => $session,
                    'openid' => $openid,
                );
                return BaseController::SuccessRespone($data);
                exit(0);
            }else{
                $data = array(
                    'login' => false,//还没完成登录录入数据库
                    'session' => $session,
                    'openid' => $openid,
                );
                return BaseController::SuccessRespone($data);
                exit(0);
            }
        }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }
        exit(0);
    }

    public function actionGetnumber(){
        $wxdeal = new Deal();
        $redis = Yii::$app->redis;
        if( Yii::$app->request->isPost ){
            $postData = Yii::$app->request->post();
            $session = $postData['session'];
            $encryptedData = $postData['encryptedData'];
            $iv = $postData['iv'];
            $key = '_3rd_session:' . $session;
            $result = json_decode($redis->get($key),true);
            $openid = $result['openid'];                        
            $session_key = $result['session_key'];
            if( empty($session_key) ){
                $content = "没有找到session_key,请尝试重新登录";
                $data = array(
                    '0' => -3,
                    '1' => $content,
                );      
                return BaseController::FailRespone($data);
                exit(0);
            }       

            //判断encryptedData、iv是否为空
            if( empty($encryptedData) ||  empty($iv) ){
                    $telephone = $postData['phone'];
                    $data = array(
                         'openid' => $openid,
                         'telephone' => $telephone,
                    );
                    $result = GameUser::phoneAdd($data);
                    if ( $result == 1){
                        $data = "更新成功";
                        return BaseController::SuccessRespone($data);
                        exit(0);
                     }else if ($result == -1){
                        $content = "更新失败";
                        $data = array(
                           '0' => -1,
                           '1' => $content,
                          );
                         return BaseController::FailRespone($data);
                         exit(0);
                     }
            }else{
            $data = $wxdeal->getPhoneinfo( $session_key, $encryptedData, $iv );
            if ( $data != -3 ) {
                $userdata = array(
                    'openid' => $openid,
                    'telephone' => $data['phoneNumber'],
                );
                $code = GameUser::phoneAdd($userdata);
                if( $code == 1 ){
                    $data = $openid;
                    return BaseController::SuccessRespone($data);
                    exit(0);
                }else if( $code == -1 ){
                    $content = "请尝试重新登录";
                    //数据库录入失败
                    $data = array(
                        '0' => -1,
                        '1' => $content,
                    );
                    return BaseController::FailRespone($data);
                    exit(0);
                }
            }else{
                $error = $data;
                $data = array(
                    '0' => 0,
                    '1' => $error,
                );
                return BaseController::FailRespone($data);
            }
         } 
        }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }
        exit(0);
      }
 
    public function actionUserinfo(){
        $wxdeal = new Deal();
        $redis = Yii::$app->redis;
        if( Yii::$app->request->isPost ){
            $postData = Yii::$app->request->post();
            $session = $postData['session'];
            $encryptedData = $postData['encryptedData'];
            $iv = $postData['iv'];
            if( empty($session) || empty($encryptedData) || empty($iv) ){
                $content = "请尝试重新登录";
                $data = array(
                    '0' => -2,
                    '1' => $content,
                );
                return BaseController::FailRespone($data); 
                exit(0);          
            }
            $key = '_3rd_session:' . $session;
            $result = json_decode($redis->get($key),true);
            $openid = $result['openid'];
            $session_key = $result['session_key'];
            if( empty($session_key) ){
                $content = "没有找到session_key,请尝试重新登录";
                $data = array(
                    '0' => -3,
                    '1' => $content,
                );
                return BaseController::FailRespone($data);
                exit(0);
            }
            $data = $wxdeal->getUserinfo( $session_key, $encryptedData, $iv );
            if ( $data != -1 && $data != -2 && $data != -3) {
                $userdata = array(
                    'openid' => $openid,
                    'nickname' => $data['nickName'],
                    'avatarUrl' => $data['avatarUrl'],
                );
                $code = GameUser::judgedata_new($userdata);
                if( $code ){
                    $data = $openid;
                    return BaseController::SuccessRespone($data);
                    exit(0);
                }else if( $code == -1 ){
                    $content = "请尝试重新登录";
                    //数据库录入失败
                    $data = array(
                        '0' => -1,
                        '1' => $content,
                    );
                    return BaseController::FailRespone($data);
                    exit(0);
                }
            }else{
                $error = $data;
                $data = array(
                    '0' => 0,
                    '1' => $error,
                );
                return BaseController::FailRespone($data);
            }
        }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }        
        exit(0);
    }

    //小程序确认已登陆用户头像和昵称是否一致
    public function actionCheckan(){
        $redis = Yii::$app->redis;
        if( Yii::$app->request->isPost ){
            $postData = Yii::$app->request->post();
            $session = $postData['session'];
            $avatarurl = $postData['avatarurl'];
            $nickname = $postData['nickname'];
            $key = '_3rd_session:' . $session;
            $result = json_decode($redis->get($key),true);
            $openid = $result['openid'];
            $userdata = array(
                'openid' => $openid,
                'avatarUrl' => $avatarurl,
                'nickname' => $nickname,
            );
            $code = GameUser::judgedata_new($userdata);
            if( $code ){
                $data = "更新成功";
                return BaseController::SuccessRespone($data);
                exit(0);
            }else if( $code == -1 ){
                $content = "更新失败";
                //数据库录入失败
                $data = array(
                    '0' => -1,
                    '1' => $content,
                );
                return BaseController::FailRespone($data);
                exit(0);
            }
        }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }
        exit(0);
    }


}
